package casts.misc
{
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.net.URLRequest;
   import flash.text.TextField;
   // fb
   import com.facebook.graph.Facebook;
   // greensock
   import com.greensock.TweenMax;
   import com.greensock.events.LoaderEvent;
   import com.greensock.loading.LoaderMax;
   import com.greensock.loading.ImageLoader;

   /**
    * Facebook選擇朋友按鈕
    * @author cjboy | cjboy1984@gmail.com
    * @usage
      // init
      FriendBox.init();

      // create
      for(var i:uint = 0; i < 100; ++i)
      {
         var box:FriendBox = new FriendBox();
         box.tfName.text = 'xxx';
         box.setInfo('yyy', 'id');
      }

      // load
      FriendBox.load();

      // get
      trace(FriendBox.selections);
      trace(FriendBox.selections[0].uid);
    */
   public class FriendBox extends MovieClip
   {
      // fla
      public var tfName:TextField;

      // 同時最多被選重的數量
      private const MAX_SELECTIONS:int = 3;
      // 被選中的
      public static var selections:Vector.<FriendBox> = new Vector.<FriendBox>();

      // 這個元件對到的facebook id
      public var uid:String;
      // 小圖loader
      private var loader:ImageLoader;

      // Loader的總管
      public static var loaderMax:LoaderMax;

      /* constructor */
      public function FriendBox()
      {
         mouseChildren = false;

         stop();

         addEventListener(Event.ADDED_TO_STAGE, onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
      }

      // --------------------- LINE ---------------------

      // 設定圖、uid
      public function setInfo(url:String, id:String):void
      {
         // save id
         uid = id;

         // 初始化ImageLoader，並設好x,y,alpha
         loader = new ImageLoader(url, { x:0, y:0, alpha:0, container:this } );
         loader.addEventListener(LoaderEvent.PROGRESS, onPictureLoading);
         loader.addEventListener(LoaderEvent.COMPLETE, onPictureLoaded);

         loaderMax.append(loader);
      }

      // --------------------- LINE ---------------------

      // 初始化LoaderMax
      public static function init():void
      {
         loaderMax = new LoaderMax();
         loaderMax.maxConnections = 5;
      }

      // 取消所有loading工作
      public static function destroy():void
      {
         loaderMax.cancel();
         loaderMax.unload();
         loaderMax = null;
      }

      // 開始loading工作
      public static function load():void
      {
         loaderMax.load();
      }

      // --------------------- LINE ---------------------

      // ################### protected ##################

      // #################### private ###################

      private function onAdd(e:Event):void
      {
         buttonMode = false;

         addEventListener(MouseEvent.CLICK, onClick);
         addEventListener(MouseEvent.ROLL_OVER, onOver);
         addEventListener(MouseEvent.ROLL_OUT, onOut);
      }

      private function onRemove(e:Event):void
      {
         TweenMax.killTweensOf(this);

         if (loader)
         {
            loader.removeEventListener(LoaderEvent.PROGRESS, onPictureLoading);
            loader.removeEventListener(LoaderEvent.COMPLETE, onPictureLoaded);
            loader.cancel();
            loader.unload();
            loader = null;
         }

         removeEventListener(MouseEvent.CLICK, onClick);
         removeEventListener(MouseEvent.ROLL_OVER, onOver);
         removeEventListener(MouseEvent.ROLL_OUT, onOut);
      }

      // --------------------- LINE ---------------------

      private function onClick(e:MouseEvent):void
      {
         if (!buttonMode) return;

         // 先找看看自己是否已在selections裡
         var res:int = selections.indexOf(this);
         if (res == -1)
         {
            if (selections.length < MAX_SELECTIONS)
            {
               selections.push(this);
               gotoAndStop(2);
            }
            else
            {
               // do something
            }
         }
         else
         {
            selections.splice(res, 1);
            gotoAndStop(1);
         }
      }

      private function onOver(e:MouseEvent):void
      {
         if (!buttonMode) return;
         // DO NOTHING
      }

      private function onOut(e:MouseEvent):void
      {
         if (!buttonMode) return;
         // DO NOTHING
      }

      // --------------------- LINE ---------------------

      // 圖讀取中
      private function onPictureLoading(e:LoaderEvent):void
      {
      }

      // 圖讀完
      private function onPictureLoaded(e:LoaderEvent):void
      {
         loader.removeEventListener(LoaderEvent.PROGRESS, onPictureLoading);
         loader.removeEventListener(LoaderEvent.COMPLETE, onPictureLoaded);

         // 淡入圖
         TweenMax.to(loader.content, 0.5, { alpha:1 } );

         buttonMode = true;
      }

      // --------------------- LINE ---------------------

   }

}